package cn.dylan.org.controller;

import cn.dylan.basic.exception.BusinessException;
import cn.dylan.basic.util.ExcelUtils;
import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.PageList;
import cn.dylan.org.domain.Shop;
import cn.dylan.org.domain.ShopLog;
import cn.dylan.org.dto.ShopDto;
import cn.dylan.org.query.ShopQuery;
import cn.dylan.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API",description="相关的CRUD功能")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     *
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }

    /**
     * GetMapping 请求
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id") Long id){
        return shopService.LoadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个部门" )
    public JsonResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            shopService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 修改和添加
     * @param shop
     * @return
     */
    @PostMapping
    @ApiOperation(value = "修改或者添加" )
    public JsonResult updateOrAdd(@RequestBody Shop shop){
        try{
            if(shop.getId()==null){
                //添加操作
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PutMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Shop> QueryPage(@RequestBody ShopQuery query){
        try {
            return shopService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException s){
            s.printStackTrace();
            return JsonResult.me().setMsg("入驻失败!"+s.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
     * 审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过" )
    public JsonResult pass(@RequestBody ShopLog log){
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }


    /**
     * 驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "驳回" )
    public JsonResult reject(@RequestBody ShopLog log){

        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            // 查询所有shop数据
            List<Shop> list = shopService.loadAll();
            // d调用工具了  (List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 导入  ，调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass

            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            // 保存到数据库表中
            list.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * domain，pojo.： 实体类----》跟数据库中的表对应的
     * query:  做查询参数使用
     * dto: 接收参数或者传参使用的
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }

}
